package android.rockchip.update.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class Preferences implements Constants {

    public static final String TAG = "Preferences";

    public static String PREF_NAME = "OTAUpdateSettings";

    private Preferences() {
    }

    private static SharedPreferences getPrefs(Context context) {
        return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static boolean getIsDownloadOnGoing(Context context) {
        return getPrefs(context).getBoolean(DOWNLOAD_RUNNING, false);
    }

    public static long getDownloadID(Context context) {
        return getPrefs(context).getLong(DOWNLOAD_ID, 0L);
    }

    public static boolean getBackgroundService(Context context) {
        Log.d(TAG, "Background Service set to " + getPrefs(context).getBoolean(UPDATER_BACK_SERVICE, true));
        return getPrefs(context).getBoolean(UPDATER_BACK_SERVICE, true);
    }

    public static int getBackgroundFrequency(Context context) {
        return Integer.parseInt(getPrefs(context).getString(UPDATER_BACK_FREQ, "21600"));
    }

    public static void setDownloadFinished(Context context, boolean value) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putBoolean(IS_DOWNLOAD_FINISHED, value);
        editor.commit();
    }

    public static void setMD5Passed(Context context, boolean value) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putBoolean(MD5_PASSED, value);
        editor.commit();
    }

    public static void setHasMD5Run(Context context, boolean value) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putBoolean(MD5_RUN, value);
        editor.commit();
    }

    public static void setIsDownloadRunning(Context context, boolean value) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putBoolean(DOWNLOAD_RUNNING, value);
        editor.commit();
    }

    public static void setDownloadID(Context context, long value) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putLong(DOWNLOAD_ID, value);
        editor.commit();
    }

    public static void setIgnoredRelease(Context context, String value) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putString(IGNORE_RELEASE_VERSION, value);
        editor.commit();
    }

    public static void setFirstRun(Context context, boolean value) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putBoolean(FIRST_RUN, value);
        editor.commit();
    }

    public static void setUpdateDescription(Context context, String value) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putString(UPDATE_DESTRIPTION, value);
        editor.commit();
    }

    public static String getUpdateDescription(Context context) {
        return getPrefs(context).getString(UPDATE_DESTRIPTION, "");
    }

}